/**
 * Created by hang on 14-5-10.
 */
var pageNum = 0;
var host = "";
$(document).ready(function () {
	host = "/xipujewelWeb";
    $("#search_btn").click(function () {
        search(0);
    });

    setShopCartCount();

});

function setShopCartCount(){
	//alert(host + "/ShopCart/getShopCartCount");
    $.post(host + "/ShopCart/getShopCartCount",function (data) {
        if (data != null) {
	//	alert(data);
            $("#shopCartCount").text(data);
            $("#shopCartCount2").text(data);
        }
    });
}


function addShopCar(id) {
//    alert(id);
//    alert(host);
    $.post(host + "/jewel/addShopCar", {id: id}
      /*  function (data) {
            if (data != null) {
                alert(data);
                search(0);
            }
        }*/);
}


function addShopCars() {
    var chk_value = [];
    $('input[name="jewelItem"]:checked').each(function () {
        if ($(this).val() != '') {
            chk_value.push($(this).val());
        }
    });
//            alert(chk_value);
    if (chk_value.length == 0) {
        alert("请选择钻石");
    } else {
        $.post(host + "/jewel/addShopCars", {ids: chk_value.toString()},
            function (data) {
                if (data != null) {
                    alert(data);
                    search(0);
                    setShopCartCount();
                }
            });
    }
}

function search(isLoadMore) {
    if (isLoadMore == 1) {
        $("#moreTr").children().text("正在加载，请稍后...");
        pageNum++;
    } else {
        $("#searchLoading").text("正在搜索，请稍后...");
        pageNum = 0;
    }
    var shape = "";//形状
    var color = "";//颜色
    var cut = ""; // 切工
    var neatness = "";//净度
    var buffing = "";//抛光
    var fluorescence = "";//荧光
    var card = "";//证书
    var symmetry = "";//对称
    var order = "";//排序
    var holdType = "";//库存
    var num = $("input[name='num']").val();//货号
    var cardId = $("input[name='cardId']").val();//证书号
    var startPrice = $("input[name='startPrice']").val();
    var endPrice = $("input[name='endPrice']").val();
    var startWeight = $("input[name='startWeight']").val();
    var endtWeight = $("input[name='endWeight']").val();

    order = $(".cur.a1").text();
//    holdType = $(".cur.a2").text();
    holdType = "";
    $(".cur[name='search_shape']").each(function () {
        shape = shape + $(this).text().toString() + ",";
    });
    $(".cur[name='search_color']").each(function () {
        color = color + $(this).text().toString() + ",";
    });
    $(".cur[name='search_cut']").each(function () {
        cut = cut + $(this).text().toString() + ",";
    });
    $(".cur[name='search_neatness']").each(function () {
        neatness = neatness + $(this).text().toString() + ",";
    });
    $(".cur[name='search_buffing']").each(function () {
        buffing = buffing + $(this).text().toString() + ",";
    });
    $(".cur[name='search_fluorescence']").each(function () {
        fluorescence = fluorescence + $(this).text().toString() + ",";
    });
    $(".cur[name='search_card']").each(function () {
        card = card + $(this).text().toString() + ",";
    });
    $(".cur[name='search_symmetry']").each(function () {
        symmetry = symmetry + $(this).text().toString() + ",";
    });
    $(".cur[name='search_holdType']").each(function () {
        holdType = holdType + $(this).text().toString() + ",";
    });
    shape = shape.substring(0, shape.lastIndexOf(","));
    color = color.substring(0, color.lastIndexOf(","));
    cut = cut.substring(0, cut.lastIndexOf(","));
    neatness = neatness.substring(0, neatness.lastIndexOf(","));
    buffing = buffing.substring(0, buffing.lastIndexOf(","));
    fluorescence = fluorescence.substring(0, fluorescence.lastIndexOf(","));
    card = card.substring(0, card.lastIndexOf(","));
    symmetry = symmetry.substring(0, symmetry.lastIndexOf(","));
    holdType = holdType.substring(0, holdType.lastIndexOf(","));
//    alert(shape);
//    alert(host);
    $.post(host + "/jewel/search",
        {filter_holdType: holdType, filter_order: order, filter_shape: shape, filter_color: color,
            filter_cut: cut, filter_neatness: neatness, filter_buffing: buffing,
            filter_fluorescence: fluorescence, filter_card: card, filter_symmetry: symmetry,
            filter_num: num, filter_cardId: cardId, filter_startPrice: startPrice,
            filter_endPrice: endPrice, filter_startWeight: startWeight,
            filter_endWeight: endtWeight, page: pageNum, isLoadMore: isLoadMore},
        function (data) {
            if (isLoadMore == 0) {
                $("#searchLoading").text("");
                $(data).replaceAll($("#jewel_results"));
            } else {
                $("#moreTr").children().text("点击加载更多");
                $(data).insertBefore($("#moreTr"));
            }
        });
}
